package com.apexwear.apexutils.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apex.jieli.watcha.JieliWatchInfo;
import com.apexwear.apexutils.R;
import com.apexwear.apexutils.adapter.AdapterDial;
import com.apexwear.apexutils.data.Jieli707Dial;
import com.apexwear.apexutils.databinding.Activity707DialBinding;
import com.google.gson.Gson;

import java.util.List;

public class Delete707DialActivity extends AppCompatActivity {
    final String TAG = this.getClass().getSimpleName();
    List<JieliWatchInfo> infos;
    Activity707DialBinding dataBinding;
    Jieli707Dial jieli707Dial;
    AdapterDial adapterDial;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_707_dial);
        dataBinding.tooBar.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(dataBinding.tooBar);
        getSupportActionBar().setTitle(null);
        dataBinding.tooBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Delete707DialActivity.this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        dataBinding.otaList.setLayoutManager(linearLayoutManager);
        jieli707Dial = new ViewModelProvider(this).get(Jieli707Dial.class);
        jieli707Dial.infoLive.observe(this, new Observer<List<JieliWatchInfo>>() {
            @Override
            public void onChanged(List<JieliWatchInfo> jieliWatchInfos) {
                Log.i(TAG, "infos:" + new Gson().toJson(jieliWatchInfos));
                if (jieliWatchInfos != null && !jieliWatchInfos.isEmpty()) {
                    dataBinding.delete.setVisibility(View.VISIBLE);
                } else {
                    dataBinding.delete.setVisibility(View.GONE);
                }
                adapterDial = new AdapterDial(jieliWatchInfos);
                dataBinding.otaList.setAdapter(adapterDial);
            }
        });
        dataBinding.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterDial != null) {
                    jieli707Dial.deleteWatchface(adapterDial.getDeleteWatchFace());
                }

            }
        });
        jieli707Dial.getWatchFaceData();
    }
}
